//
//  kqy1essentialViewController.m
//  百思不得姐os1
//
//  Created by kqy on 8/1/16.
//  Copyright © 2016 kqy. All rights reserved.
//

#import "kqy1essentialViewController.h"
#import "kqy1AllTableViewController.h"
#import "kqy2VideoTableViewController.h"
#import "kqy3VoicesTableViewController.h"
#import "kqy4commentTableViewController.h"
#import "kqy5PicTableViewController.h"
@interface kqy1essentialViewController () <UIScrollViewDelegate>
@property (nonatomic, weak) UIView *titleView;
@property (nonatomic, weak)  UIButton *chooseButton;
@property (nonatomic, weak) UIView *indicatorView;
@property (nonatomic, weak) UIScrollView *mainScrollView;


@end

@implementation kqy1essentialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kqyRandomColor;
    
    [self setupTabBarItem];
    [self addChildVC];
    [self setupScrollView];
    [self setupTitleView];
    [self addChildVcAfterScrollView];
}
- (void) addChildVC {
    kqy1AllTableViewController *all1VC = [[kqy1AllTableViewController alloc] init];
    [self addChildViewController:all1VC];
    
    kqy2VideoTableViewController *video2VC = [[kqy2VideoTableViewController alloc] init];
    [self addChildViewController:video2VC];
    
    kqy3VoicesTableViewController *voice3VC = [[kqy3VoicesTableViewController alloc] init];
    [self addChildViewController:voice3VC];
    
    kqy4commentTableViewController *comment4VC = [[kqy4commentTableViewController alloc] init];
    [self addChildViewController:comment4VC ];
    kqy5PicTableViewController *pic5VC = [[kqy5PicTableViewController alloc] init];
    [self addChildViewController:pic5VC];
    
    
}
- (void) setupScrollView {
    UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _mainScrollView = mainScrollView;
    [self.view addSubview:mainScrollView];
    mainScrollView.delegate = self;
    mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 5, 0);
    mainScrollView.pagingEnabled = YES;
    
}

//
//
//
- (void) setupTitleView {
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 40)];
    titleView.backgroundColor = [UIColor purpleColor];
    _titleView = titleView;
    [self.view addSubview:titleView];
    CGFloat buttonWidth = self.view.frame.size.width / 5;
    CGFloat buttonHeight = 39;
    NSArray *titleArray = @[@"all",@"videos",@"voices",@"commen",@"pic"];
    for (int i = 0; i < 5; ++i) {
        UIButton *titButton = [[UIButton alloc] init];
        titButton.frame = CGRectMake(i * buttonWidth, 0, buttonWidth, buttonHeight);
        titButton.tag = i + 100;
        [titButton setTitle:titleArray[i] forState:UIControlStateNormal];
        [titleView addSubview:titButton];
        [titButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [titButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [titButton addTarget:self action:@selector(titBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor redColor];
    _indicatorView = indicatorView;
    [titleView addSubview:indicatorView];
//    CGPoint tempIndicaotrY = indicatorView.frame;
//    tempIndicaotrY.y
    CGRect tempIndicatorY = indicatorView.frame;
    tempIndicatorY.origin.y = 38;
    tempIndicatorY.size.height = 1;
    indicatorView.frame = tempIndicatorY;
    //开始时线的位置
    UIButton *firstButton = (UIButton *)[self.view viewWithTag:100];
    [firstButton.titleLabel sizeToFit];
//    [self titBtnClick:firstButton];
    firstButton.selected = YES;
    _chooseButton = firstButton;
    CGFloat titleWidth = [firstButton.currentTitle sizeWithAttributes:@{NSFontAttributeName:firstButton.titleLabel.font}].width;
     CGRect tempWidthFrame = _indicatorView.frame;
    tempWidthFrame.size.width = titleWidth + 4;
    _indicatorView.frame = tempWidthFrame;
    CGPoint tempIndicatorView = _indicatorView.center;
    tempIndicatorView.x = firstButton.center.x;
    _indicatorView.center = tempIndicatorView;
    
}
- (void) titBtnClick:(UIButton *)button {
    _chooseButton.selected = NO;
    button.selected = YES;
    _chooseButton = button;
    CGFloat titleWidth = [button.currentTitle sizeWithAttributes:@{NSFontAttributeName:button.titleLabel.font}].width;
    [UIView animateWithDuration:0.5 animations:^{
        CGRect tempWidthFrame = _indicatorView.frame;
        tempWidthFrame.size.width = titleWidth + 4;
        _indicatorView.frame = tempWidthFrame;
        CGPoint tempIndicatorView = _indicatorView.center;
        tempIndicatorView.x = button.center.x;
        _indicatorView.center = tempIndicatorView;
        
    }];
    
    CGPoint scrollOffset = _mainScrollView.contentOffset;
    NSInteger pageIndex = button.tag - 100;
    scrollOffset.x = pageIndex * self.view.frame.size.width;
//    [_mainScrollView setContentOffset:scrollOffset];
    [_mainScrollView setContentOffset:scrollOffset animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self addChildVcAfterScrollView];
    
    CGPoint scrollOffset = _mainScrollView.contentOffset;
    NSInteger pageIndex = scrollOffset.x / self.view.frame.size.width;
    UIButton *moveButton = _titleView.subviews[pageIndex];
    [self titBtnClick:moveButton];
   
    
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
   
    [self addChildVcAfterScrollView ];
}

- (void) addChildVcAfterScrollView {
    CGPoint scrollOffset = _mainScrollView.contentOffset;
    NSInteger pageIndex = scrollOffset.x / self.view.frame.size.width;
    UIViewController *childVC = self.childViewControllers[pageIndex];
    
    childVC.view.frame = self.mainScrollView.bounds;
    
    [_mainScrollView addSubview:childVC.view];
    
}
- (void) setupTabBarItem {
    UITabBarItem *meTabBarItem = [[UITabBarItem alloc] initWithTitle:@"me" image:[UIImage imageNamed:@"MainTagSubIcon"] selectedImage:[UIImage imageNamed:@"MainTabSubIconClick"]];
    UIBarButtonItem *meBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"meBar" style:UIBarButtonItemStyleDone target:self action:@selector(meBarClick)];
    self.navigationItem.leftBarButtonItem = meBarButtonItem;
}
- (void) meBarClick {
    NSLog(@"me bar  click");
}


@end
